var id = "";
var userId = "";
var renderIs = ""; //判断特定状态下可以进行文件上传

layui.use(['table', 'admin', 'ax', 'func', 'form', 'upload', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var upload = layui.upload;
    var layer = layui.layer;

    /**
     * 获取当前数据对比接口的id
     */
    function getId() {
        id = sessionStorage.getItem("id")
        // console.log(id)
        sessionStorage.clear();
    }

    $(function () {
        getId();
    })


    /**
     * 获取当前id的数据对比接口详情并填充表单
     */
    var ajax = new $ax(Feng.ctxPath + "/interfaceapproval/list?id=" + id);
    var result = ajax.start();
    // console.log(result.data[0])
    var data = result.data[0]
    var remarks = data.remarks;
    if (data.state == 0) {
        data.state = "等待审核"
    } else if (data.state == 1) {
        data.state = "审核通过"
    } else if (data.state == 2) {
        data.state = "已驳回"
    } else if (data.state == 3) {
        data.state = "已完成"
    }
    form.val('interfaceForm', data);


    /**
     * 根据状态追加按钮操作
     */
    // console.log(data)
    if (data.state == "等待审核") {
        $("#buttonReply").empty();
        var a = '<button class="layui-btn layui-btn-normal" id="approve" type="button">&nbsp;批准&nbsp;</button>';
        a += '<button class="layui-btn layui-btn-normal" id="reject" type="button">&nbsp;驳回&nbsp;</button>';
        $("#buttonReply").append(a)
        $("#btnSubmit").attr("disabled", true)
        $("#remark").attr("disabled", true)
        $("#uploadFile").attr("disabled", true)
        renderIs = false;
    } else if (data.state == "审核通过") {
        $("#buttonReply").empty();
        $("#btnSubmit").attr("disabled", false)
        $("#remark").attr("disabled", false)
        renderIs = true;
    } else if (data.state == "已驳回") {
        $("#buttonReply").empty();
        $("#btnSubmit").attr("disabled", true)
        $("#remark").attr("disabled", true)
        renderIs = false;
    } else if (data.state == "已完成") {
        $("#buttonReply").empty();
        $("#btnSubmit").attr("disabled", true)
        $("#remark").attr("disabled", true)
        renderIs = false;
        $.ajax(Feng.ctxPath + '/data/list', {
            data: {
                userId: data.userId,
                messageId: data.id,
            },
            success: function (res) {
                // console.log(res)
                $("#remark").val(res[0].remark)
                $("#fileName")[0].innerHTML = res[0].fileName
                $("#iconFile")[0].innerHTML = '&#xe621;'
            }
        })
    }

    /**
     * 批准和驳回
     */
    var ids = new Array();
    // console.log(id)
    ids.push(id)
    // console.log(ids)
    $("#buttonReply").on('click', '#approve', function () {
        sessionStorage.setItem("id", id);
        $.ajax(Feng.ctxPath + '/interfaceapproval/approve', {
            data: {
                ids: ids
            },
            traditional: true,
            success: function () {
                Feng.success('批准成功')
                location.reload()
            },
            error: function () {
                Feng.error('批准失败')
            }
        })
    })

    $("#buttonReply").on('click', '#reject', function () {
        sessionStorage.setItem("id", id);
        $.ajax(Feng.ctxPath + '/interfaceapproval/reject', {
            data: {
                ids: ids
            },
            traditional: true,
            success: function (res) {
                // console.log(res)
                if (res == ids.length) {
                    Feng.success('驳回成功')
                    location.reload()
                }
            },
            error: function () {
                Feng.error('驳回失败')
            }
        })
    })

    /**
     * 文件上传
     */
    if (renderIs) {
        upload.render({
            elem: '#uploadFile'
            , url: Feng.ctxPath + '/data/uploadfile' //改成您自己的上传接口
            , accept: 'file' //普通文件
            , method: 'POST'
            , data: {
                remark: function () {
                    return $("#remark").val()
                },
                userId: function () {
                    return data.userId
                },
                messageId: function () {
                    return data.id
                }
            }
            , auto: false //禁止自动上传
            , size: 1024 //限制文件大小，单位 KB
            , bindAction: '#btnSubmit'
            , done: function (res) {
                Feng.success(res.message);
                parent.location.reload();
                console.log(res);
            }
        });
    }

    /**
     * 点击提交
     */
    $("#btnSubmit").click(function () {
        $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
            data: {
                id: data.id,
                userId: data.userId,
                interfaceId: data.interfaceId,
                content: data.content,
                serviceid: data.serviceid,
                department: data.department,
                echo: data.echo,
                description: data.description,
                system: data.system,
                servicetype: data.servicetype,
                idescribe: data.idescribe,
                state: 3,
                remarks: data.remarks,
                revision: data.revision,
                createdBy: data.createdBy,
                createdTime: data.createdTime,
                updatedBy: data.updatedBy,
                updatedTime: data.updatedTime
            },
            success: function (res) {
                Feng.success(res)
                parent.location.reload()
            }
        })
    })


    /**
     * 文本域高度自适应
     */
    //jQuery实现textarea高度根据内容自适应
    $.fn.extend({
        txtaAutoHeight: function () {
            return this.each(function () {
                var $this = $(this);
                if (!$this.attr('initAttrH')) {
                }
                setAutoHeight(this).on('input', function () {
                    setAutoHeight(this);
                });
            });

            function setAutoHeight(elem) {
                var $obj = $(elem);
                return $obj.css({height: $obj.attr('initAttrH'), 'overflow-y': 'hidden'}).height(elem.scrollHeight);
            }
        }
    });
    //调用
    $(function () {
        $("#description").txtaAutoHeight();
    });


});
